package com.zbkj.crmeb.chant.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbkj.crmeb.chant.dao.ChannelPartitionUserDao;
import com.zbkj.crmeb.chant.model.ChannelPartitionUser;
import com.zbkj.crmeb.chant.service.ChannelPartitionUserService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

@Service
public class ChannelPartitionUserServiceImpl extends ServiceImpl<ChannelPartitionUserDao, ChannelPartitionUser> implements ChannelPartitionUserService {

    @Override
    public Integer getUserPartitionTime(Integer uid, Integer partitionId) {
        List<ChannelPartitionUser> channelPartitionUsers = this.list(Wrappers.<ChannelPartitionUser>lambdaQuery().eq(ChannelPartitionUser::getUid, uid)
                .eq(ChannelPartitionUser::getPartitionId, partitionId));
        return channelPartitionUsers.size();
    }

    @Override
    public Boolean checkUserPartitionStatus(Integer uid, Integer partitionId) {
        ChannelPartitionUser one = this.getOne(Wrappers.<ChannelPartitionUser>lambdaQuery().eq(ChannelPartitionUser::getUid, uid)
                .eq(ChannelPartitionUser::getPartitionId, partitionId)
                .orderByDesc(ChannelPartitionUser::getCreateTime)
                .last("limit 1"));
        return Objects.nonNull(one);
    }

    @Override
    public ChannelPartitionUser getPartitionUser(Integer uid, Integer partitionId) {
        return this.getOne(Wrappers.<ChannelPartitionUser>lambdaQuery().eq(ChannelPartitionUser::getUid, uid)
                .eq(ChannelPartitionUser::getPartitionId, partitionId)
                .orderByDesc(ChannelPartitionUser::getCreateTime)
                .last("limit 1"));
    }

    @Override
    public Boolean saveUserPartition(Integer uid, Integer partitionId, BigDecimal useCondition, BigDecimal partitionMoney, Integer useType) {
        ChannelPartitionUser channelPartitionUser = new ChannelPartitionUser();
        channelPartitionUser.setPartitionId(partitionId);
        channelPartitionUser.setUseCondition(useCondition);
        channelPartitionUser.setUid(uid);
        channelPartitionUser.setPartitionMoney(partitionMoney);
        channelPartitionUser.setUseType(useType);
        return this.save(channelPartitionUser);
    }
}
